'''
Created on Apr 9, 2012

@author: loic
'''

from mp3rename import *
#from eyeD3.utils import TRACE
from glob import glob
from optparse import OptionParser
import os.path, sys

if __name__ == '__main__':
    parser = OptionParser()
    parser.add_option("-a", "--artist", dest="artist",
                  help="fix artist", action="store_true", default=False)
    parser.add_option("-t", "--title", dest="title",
                  help="fix title", action="store_true", default=False)
    parser.add_option("-b", "--album", dest="album",
                  help="fix album", action="store_true", default=False)
    parser.add_option("-1", "--id3v1", dest="id3v1",
                  help="handle id3 v1 tags", action="store_true", default=False)
    parser.add_option("-2", "--id3v2", dest="id3v2",
                  help="handle id3 v2 tags", action="store_true", default=False)
    parser.add_option("-v", "--verbose", dest="verbose",
                  help="verbose", action="store_true", default=False)
    parser.add_option("-d", "--debug", dest="debug",
                  help="debug", action="store_true", default=False)
    parser.add_option("-l", "--list", dest="list",
                  help="list files and print the Author& Title", action="store_true", default=False)
    parser.add_option("-f", "--dir", dest="dir",
                  help="directory containing mp3s")
    (options, args) = parser.parse_args()

    if options.verbose:
        print "Options:"
        print options
        print "Args:"
        print args
        
    if options.debug:
        eyeD3.utils.TRACE=1

    if options.id3v1:
        id3_v = ID3_V1
    elif options.id3v2:
        id3_v = ID3_V2
    else:
        id3_v = ID3_ANY_VERSION
       
    filelist = []
    if options.dir:
        filelist = glob(options.dir+'/*.mp3')
        print "Chemin : %s" % options.dir+'/*.mp3'
    else:
        for a in args:
            (root, ext) = os.path.splitext(a)
            if ext == '.mp3':
                filelist.append(a)
            else:
                print "%s does not have the mp3 extention (%s / %s)" % (a, root, ext)
    filelist.sort()
    tag = mp3()
    tags = {}
    
    for f in filelist:
        tag.link(f, id3_v)
        if options.verbose or options.list:
            print "Fichier : %s" % f
        if options.list:
            frames = tag.get_frames_as_text()
            for f in frames:
                try:
                    print "%s : %s" % (f, frames[f])
                except:
                    pass
        else:
            tags[f] = {}
            tags[f]['artist'] = tag.getArtist()
            tags[f]['title'] = tag.getTitle()
            tags[f]['album'] = tag.getAlbum()
        
    if options.list:  
        sys.exit()
            
    if options.verbose:
        print "2eme tour"
        
    eyeD3.frames.FORCE_ENCODING = eyeD3.frames.UTF_8_ENCODING
    for f in filelist:
        if options.verbose:
            print "Fichier : %s" % f
        tag.link(f, id3_v)
        artist_changed = title_changed = album_changed = something_to_update = False
        a = tag.getArtist()
        t = tag.getTitle()
        alb = tag.getAlbum()
        if a != tags[f]['artist']:
            print "Fichier : %s" % f
            print "Artiste : %s != %s\n" %(tags[f]['artist'], a)
            artist_changed = True
        if t != tags[f]['title']:
            if not artist_changed:
                print "Fichier : %s" % f
            print "Titre : %s != %s\n" % (tags[f]['title'], t)
            title_changed = True
        if alb != tags[f]['album']:
            if not artist_changed or title_changed:
                print "Fichier : %s" % f
            print "Album : %s != %s\n" % (tags[f]['album'], alb)
            album_changed = True
        if artist_changed and options.title:
            tag.setArtist(a)
            something_to_update = True
        if title_changed and options.title:
            tag.setTitle(t)
            something_to_update = True
        if album_changed and options.album:
            tag.setAlbum(alb)
            something_to_update = True
        if something_to_update:
            tag.update()